home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / c / dice-3.16.lha / examples / Uprev / UpRev.c < prev    next >
C/C++ Source or Header  |  1998-09-27  |  4KB  |  149 lines

  1. #include <stdio.h>
  2. #include <strings.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include "UpRev_rev.h"
  6.  
  7. #define MAX_FNAME 256
  8.  
  9. const static char version_string[] = VERSTAG;
  10.  
  11. int main(int argc, char **argv)
  12. {
  13.    FILE *fp;
  14.    char *vertag;
  15.    int version;
  16.    int revision;
  17.    char fname[MAX_FNAME];
  18.    char buf[80];
  19.    time_t t;
  20.    struct tm *tp;
  21.  
  22.    if ((argc > 3) || (argc < 2))
  23.    {
  24.       printf("Usage: %s <Project> [<Version>]\n", argv[0]);
  25.       return(5);
  26.    }
  27.  
  28.    vertag = "";
  29.    version =  1;
  30.    revision = 0;
  31.  
  32.    /* Determine the name of the revision file */
  33.    strncpy(fname, argv[1], MAX_FNAME-7);
  34.    fname[MAX_FNAME-7] = 0;
  35.    strcat(fname, "_rev.h");
  36.  
  37.    /* See if the file already exists. */
  38.    fp = fopen(fname, "r");
  39.    if (fp != NULL)
  40.    {
  41.       /* It exists, get the version and revision out of it */
  42.       while(fgets(buf, 80, fp) != NULL)
  43.       {
  44.          int ln;
  45.          ln = strlen(buf)-1;
  46.          if (buf[ln] == '\n') buf[ln] = 0;
  47.  
  48.          if (!memcmp(buf, "#define ", 8))
  49.          {
  50.             char *p;
  51.             p = buf+8;
  52.             while(*p == ' ' || *p == '\t') p++;
  53.             if (!memcmp(p, "VERS ", 5))
  54.             {
  55.                p += 4;
  56.                while(*p == ' ' || *p == '\t') p++;
  57.                /* Now we need to get past the name of the executable */
  58.                ln = strlen(argv[1]);
  59.                if (strlen(p) > ln)
  60.                {
  61.                   char c;
  62.                   c = p[ln];
  63.                   p[ln] = 0;
  64.                   if (stricmp(p, argv[1]))
  65.                   {
  66.                      /* We don't have a match, we need to go for the */
  67.                      /* space which separates the name               */
  68.                      p[ln] = c;
  69.                      while (*p && *p != ' ') p++;
  70.                   }
  71.                   else
  72.                   {
  73.                      /* Well, it matches, shift past the name */
  74.                      p += ln + 1;
  75.                   }
  76.                   /* Now we should be pointing past the name (and a single space) */
  77.                   /* at any aux version tag information that they might have      */
  78.                   /* we want to go from the END of the string and eliminate any   */
  79.                   /* numeric digits that are there.                               */
  80.                   ln = strlen(p);
  81.                   if ((ln > 1) && (p[ln-1] == '"')) ln--;
  82.                   while((ln > 1) && (p[ln-1] >= '0') && (p[ln-1] <= '9')) ln--;
  83.                   if ((ln > 1) && (p[ln-1] == '.'))
  84.                   {
  85.               ln--;
  86.                   while((ln > 1) && (p[ln-1] >= '0') && (p[ln-1] <= '9')) ln--;
  87.                   }
  88.                   p[ln] = 0;
  89.                   while (*p == ' ' || *p == '\t') p++;
  90.                   vertag = strdup(p);
  91.                }
  92.             }
  93.             else if (!memcmp(p, "VERSION ", 8))
  94.             {
  95.                version = atoi(p+7);
  96.             }
  97.             else if (!memcmp(p, "REVISION ", 9)) revision = atoi(p+8);
  98.          }
  99.       }
  100.  
  101.       fclose(fp);
  102.    }
  103.  
  104.    revision++;
  105.  
  106.    /* Figure out what version number we will be using */
  107.    if (argc > 2)
  108.    {
  109.       int oldver = version;
  110.       version = atoi(argv[2]);
  111.       if (version != oldver)
  112.          revision = 0;
  113.    }
  114.  
  115.    fp = fopen(fname, "w");
  116.    if (fp == NULL)
  117.    {
  118.       perror(fname);
  119.       return(20);
  120.    }
  121.  
  122.    time(&t);
  123.    tp = localtime(&t);
  124.  
  125.    tp->tm_mday;
  126.    tp->tm_year;
  127.    tp->tm_mon;
  128.  
  129.    sprintf(buf, "%d.%d.%d", tp->tm_mday, tp->tm_mon+1, tp->tm_year);
  130.  
  131.    /* Figure out what is the number to */
  132.  
  133.    fprintf(fp, "#define VERSION        %d\n",
  134.                                        version);
  135.    fprintf(fp, "#define REVISION       %3d\n",
  136.                                        revision);
  137.    fprintf(fp, "#define DATE    \"%s\"\n",
  138.                                   buf);
  139.    fprintf(fp, "#define VERS    \"%s %s%d.%d\"\n",
  140.                                  argv[1], vertag, version, revision);
  141.    fprintf(fp, "#define VSTRING \"%s %s%d.%d (%s)\"\n",
  142.                                  argv[1], vertag, version, revision, buf);
  143.    fprintf(fp, "#define VERSTAG \"\\0$%s: %s %s%d.%d (%s)\"\n",
  144.                                  "VER", argv[1], vertag, version, revision, buf);
  145.    fclose(fp);
  146.  
  147.    return(0);
  148. }
  149.